import { uploadImage } from "../../../../utils/utils";

Component({
    properties: {},
    observers: {
        'imagesInfo.**': function (data) {
            this.setData({
                imagesKeys: Object
                    .keys(data)
                    .sort((a, b) => data[a].index - data[b].index),
                disabled: Object
                    .keys(data)
                    .every(item => data[item].imgs.length >= data[item].min)
            })
        },
    },
    data: {
        disabled: false,
        imagesInfo: {
            room: {
                min: 1,
                title: '卧室',
                tips: '根据你填写的户型信息，必须上传1间卧室的照片，确保床铺、床单、枕头展示齐全'
            },
            hall: {
                min: 1,
                title: '客厅',
                tips: '拍摄时，站在厅角拍摄，视野更宽广'
            },
            wash: {
                min: 1,
                title: '卫生间',
                tips: '必须上传卫生间照片，确保马桶卫浴，地面细节的完整'
            },
            kitchen: {
                min: 0,
                title: '厨房',
                tips: '建议上传，吸引客户'
            },
            around_eat: {
                min: 1,
                title: '周边美食餐饮',
                tips: '必须上传房源周边的美食餐饮门店的照片，以此吸引更多房客前来'
            },
            around_fun: {
                min: 1,
                title: '周边旅游景点',
                tips: '必须上传房源周边的旅游景点的照片，以此吸引更多房客前来'
            },
            other: {
                min: 0,
                title: '其他',
                tips: '建议上传，可以是其它室内空间、物件、小区周边等环境。更多照片能吸引更多房客'
            },
        }
    },
    methods: {
        onReady() {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.on('getHouseImage', (data) => {
                const imagesInfo = JSON.parse(JSON.stringify(this.data.imagesInfo))
                Object.keys(data).forEach(key => {
                    imagesInfo[key] = Object.assign(imagesInfo[key], data[key])
                })
                this.setData({
                    imagesInfo
                })
            })
        },
        toTop(e) {
            const index = e.currentTarget.dataset.index
            if (index > 0) {
                const nowIndex = this.data.imagesInfo[this.data.imagesKeys[index]].index
                const upIndex = this.data.imagesInfo[this.data.imagesKeys[index - 1]].index
                this.setData({
                    ['imagesInfo.' + this.data.imagesKeys[index] + '.index']: upIndex,
                    ['imagesInfo.' + this.data.imagesKeys[index - 1] + '.index']: nowIndex,
                })
            }
        },
        uploadImage(e) {
            const key = e.currentTarget.dataset.imagekey
            uploadImage({ count: 5 - this.data.imagesInfo[key].imgs.length }).then(r => {
                this.setData({
                    ['imagesInfo.' + key + '.imgs']: [...this.data.imagesInfo[key].imgs, ...r]
                })
            })
        },
        removeImage(e) {
            const { imagekey, index } = e.currentTarget.dataset
            const data = [...this.data.imagesInfo[imagekey].imgs]
            data.splice(index, 1)
            this.setData({
                ['imagesInfo.' + imagekey + '.imgs']: data
            })
        },
        submit() {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('uploadHouseImages', this.data.imagesInfo)
            wx.navigateBack()
        }
    }
});
